package com.qiaomengapp.serviceImp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qiaomengapp.bean.Shop;
import com.qiaomengapp.bean.ShopWarp;
import com.qiaomengapp.dao.ShopMapper;
import com.qiaomengapp.exception.CustomException;
import com.qiaomengapp.service.IShopService;

@Service
public class ImpShopService implements IShopService{
	@Autowired
	private ShopMapper shopMapper;

	// 首先根据id查询表中是否具有相应的shop对象，用该对象进行错误处理
	public Shop select_ShopById(int id) throws CustomException {
		Shop shop = shopMapper.select_ShopById(id);
		if (shop == null) {
			throw new CustomException("显示Shop失败");
		} else {
			return shop;
		}
	}

	// 显示可用佣金和累计佣金
	public double select_sum_commission(int id) throws CustomException {
		double sumCommission = 0;
		Shop shop = shopMapper.select_ShopById(id);
		if (shop == null) {
			throw new CustomException("显示总佣金失败");
		} else {
			sumCommission = shopMapper.select_sum_commission(id);
		}
		return sumCommission;
	}

	public double select_able_commission(int id) throws CustomException {
		double able_commission = 0;
		Shop shop = shopMapper.select_ShopById(id);
		if (shop == null) {
			throw new CustomException("显示累计佣金失败");
		} else {
			able_commission = shopMapper.select_able_commission(id);
		}
		return able_commission;
	}

	// 更改总的佣金
	public void update_sum_addCommission(double sum, double id)
			throws CustomException {
		int i = shopMapper.update_sum_addCommission(sum, id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("增加总佣金失败");
		}
	}

	public void update_sum_reduceCommission(double sum, double id)
			throws CustomException {
		int i = shopMapper.update_sum_reduceCommission(sum, id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("减少总佣金失败");
		}
	}

	// 更改可用提现佣金
	public void update_able_addcommission(double value, double id)
			throws CustomException {
		int i = shopMapper.update_able_addCommission(value, id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("增加可用提现佣金失败");
		}
	}

	public void update_able_reduceCommission(double sum,
			double id) throws CustomException {
		int i = shopMapper.update_able_reduceCommission(sum, id);
		if (i == 1) {
			return;
		} else {
			throw new CustomException("增加可用提现佣金失败");
		}
	}

	// 店铺预览，根据id选择出相应的要显示在页面的shop
	// 字段：shop_name,shop_titleUrl,shop_level,shop_cityid,全部宝贝为shop_good中的所有商品数量,上新为在一定时间内上架的商品
	// 再连shop_good表得到gooddesc,goodimgurl
	public ShopWarp select_shopByIdForSee(int id) throws CustomException {
		ShopWarp select_shopByIdForSee = shopMapper.select_shopByIdForSee(id);
		if (select_shopByIdForSee == null) {
			throw new CustomException("选出ShopWarp失败");
		} else {
			return select_shopByIdForSee;
		}
	}
	
	//根据用户id查询商铺
	public ShopWarp select_shopByUid(int uid) throws CustomException{
		ShopWarp warp = shopMapper.select_shopByUid(uid);
		if (warp == null) {
			throw new CustomException("根据用户id查询商铺失败");
		} else {
			return warp;
		}
	}
	//根据shopName来搜索店铺,显示店铺总销量，总商品数量，shopName,shopUrl,shopLevel
	public List<ShopWarp> select_shopByShopName(String shopName)throws CustomException{
		List<ShopWarp> list = shopMapper.select_shopByShopName(shopName);
		if (list.size()==0||list==null) {
			throw new CustomException("根据shopName来搜索店铺失败");
		} else {
			return list;
		}
	}
}
